package com.itsu.spbspider.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.itsu.spbspider.entity.News;
import com.itsu.spbspider.entity.source.SourceObj;
import com.itsu.spbspider.service.Parser;
import lombok.extern.slf4j.Slf4j;
import ognl.Ognl;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class BaiduNewsParser implements Parser {


    @Override
    public List<News> parseToObj(String content) throws Exception {

        JSONObject jsonObject = JSON.parseObject(content);
        SourceObj sourceObj = null;
        Map sourceObjChannel = null;
        try {
            sourceObj = jsonObject.toJavaObject(SourceObj.class);
        } catch (Exception e) {
            sourceObjChannel = jsonObject;
        }
        List<News> list = new ArrayList<>();
        if (sourceObj != null) {
            List<SourceObj.DataBean.NewsBean> newsBeanList = sourceObj.getData().getNews();
            for (SourceObj.DataBean.NewsBean newsBean : newsBeanList) {
                News news = new News();
                BeanUtils.copyProperties(news, newsBean);
                news.setContent(JSON.toJSONString(newsBean));
                news.setLongAbs(newsBean.getLong_abs());
                if (!CollectionUtils.isEmpty(newsBean.getImageurls())) {
                    news.setImageurl1(newsBean.getImageurls().get(0).getUrl());
                    if (newsBean.getImageurls().size() >= 2)
                        news.setImageurl2(newsBean.getImageurls().get(1).getUrl());
                    if (newsBean.getImageurls().size() >= 3)
                        news.setImageurl3(newsBean.getImageurls().get(2).getUrl());
                }
                if (newsBean.getCtag() != null && StringUtils.isNotBlank(newsBean.getCtag().getName())) {
                    news.setCtag(newsBean.getCtag().getName());
                } else {
                    news.setCtag(null);
                }
                list.add(news);
            }
        } else if (sourceObjChannel != null) {
            List newsList = (List) Ognl.getValue("data.news", sourceObjChannel);
            for (Object obj : newsList) {
                News news = new News();
                news.setContent(JSON.toJSONString(obj));
                String nid = BeanUtils.getProperty(obj, "nid");
                news.setNid(nid);
                String ts = BeanUtils.getProperty(obj, "ts");
                news.setTs(ts);
                String title = BeanUtils.getProperty(obj, "title");
                news.setTitle(title);
                String url = BeanUtils.getProperty(obj, "url");
                news.setUrl(url);
                String abs = BeanUtils.getProperty(obj, "abs");
                news.setAbs(abs);
                String site = BeanUtils.getProperty(obj, "site");
                news.setSite(site);
                String long_abs = BeanUtils.getProperty(obj, "long_abs");
                news.setLongAbs(long_abs);
                List<String> imageurls = Arrays.asList(BeanUtils.getArrayProperty(obj, "imageurls"));
                if (!CollectionUtils.isEmpty(imageurls)) {
                    String imageurl1 = Ognl.getValue("url", JSON.parseObject(imageurls.get(0))).toString();
                    news.setImageurl1(imageurl1);
                    if (imageurls.size() >= 2) {
                        String imageurl2 = Ognl.getValue("url", JSON.parseObject(imageurls.get(1))).toString();
                        news.setImageurl2(imageurl2);
                    }
                    if (imageurls.size() >= 3) {
                        String imageurl3 = Ognl.getValue("url", JSON.parseObject(imageurls.get(2))).toString();
                        news.setImageurl3(imageurl3);
                    }
                }
                String ctag = BeanUtils.getProperty(obj, "ctag");
                Map ret = JSON.parseObject(ctag);
                String ctagName = Ognl.getValue("name", ret).toString();
                news.setCtag(StringUtils.isBlank(ctagName) ? null : ctagName);
                list.add(news);
            }
        }
        return list;
    }
}
